package com.online.edu.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.edu.course.entity.Chapter;
import com.online.edu.course.entity.Course;
import com.online.edu.course.entity.Vo.ChapterVo;
import com.online.edu.course.entity.Vo.PageVo;
import com.online.edu.course.service.IChapterService;
import com.online.edu.util.core.Result;
import com.online.edu.util.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 章节表 前端控制器
 * </p>
 *
 * @author world
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/course/chapter")
@Api(tags = "章节表接口")
public class ChapterController {
    @Autowired
    IChapterService chapterService;

    //获取章节列表
    @ApiOperation("根据课程id获取章节列表")
    @PostMapping("/list")
    public Result<List<ChapterVo>> list(@RequestBody PageVo pageVo){
        List<ChapterVo> chapters = chapterService.getChapters(pageVo);
        return Result.ok(chapters);
    }

/*    //添加章节
    @RequestMapping("/add")
    public Result add(Chapter chapter){
        boolean b = chapterService.save(chapter);
        if (b){
            return Result.success("添加成功！");
        }
        else {
            return Result.error("添加失败！");
        }
    }*/

/*    //编辑章节
    @ApiOperation("编辑章节")
    @GetMapping("/update")
    public Result<Chapter> update(@RequestParam("id") long id){
        Chapter chapter = chapterService.getById(id);
        return Result.ok(chapter);
    }*/

    //判断新增还是更新
    @ApiOperation("判断新增/更新")
    @PostMapping("/save/{courseId}")
    public Result save(@RequestBody Chapter chapter,
                       @PathVariable("courseId") long courseId){
        if (chapter.getId() != null){
            boolean b = chapterService.updateById(chapter);
            if (b) {
                return Result.success("更新成功！");
            }else {
                return Result.error("更新失败！");
            }
        }
        else {
            chapter.setId(Long.valueOf(StringUtil.uuid()));//设置章节id
            chapter.setCourseId(courseId);//设置课程id
            boolean b = chapterService.save(chapter);
            if (b) {
                return Result.success("新增成功！");
            }else {
                return Result.error("新增失败！");
            }
        }
    }

    //删除章节
    @ApiOperation("根据章节ID删除章节")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") long id){
        Chapter chapter = chapterService.getById(id);
        UpdateWrapper<Chapter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("logical_state",1);
        boolean b = chapterService.update(chapter, updateWrapper);
        if (b){
            return Result.success("删除成功");
        }
        else {
            return Result.error("删除失败");
        }
    }

    //批量添加章节
    @ApiOperation("批量添加章节")
    @PostMapping("/addBatch")
    public Result addBatch(List<Chapter> chapters){
        boolean b = chapterService.saveBatch(chapters);
        if (b){
            return Result.success("添加成功！");
        }
        else {
            return Result.error("添加失败！");
        }
    }
}
